package ecoiot.com.testproject.player;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alivc.player.AliVcMediaPlayer;
import com.alivc.player.MediaPlayer;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

import ecoiot.com.testproject.R;
import ecoiot.com.testproject.utils.Utils;

/**
 * Created by developer on 18-1-29.
 */

public class AliPlayerActivity extends AppCompatActivity {

    private SurfaceView mSurfaceView;
    private AliVcMediaPlayer mPlayer;

//    private String mUrl = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
    private String mUrl = "rtmp://5088.pptzb.com/1/4?auth_key=1519978880468-0-0-e2294050fe4c4a16a4fa0994c3e03406";
    private RelativeLayout mLayoutCommon;
    private RelativeLayout mLayoutList;
    private RecyclerView mRecyclerView;
    private LinearLayout mLayoutGroup;
    private ScrollView mScrollView;
    private MyHandler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_aliplayer);

        initView();

        //创建播放器的实例
        mPlayer = new AliVcMediaPlayer(this, mSurfaceView);
        mPlayer.setVideoScalingMode(MediaPlayer.VideoScalingMode.VIDEO_SCALING_MODE_SCALE_TO_FIT);
        mPlayer.setMaxBufferDuration(8000);

        if (mPlayer != null) {
            mPlayer.prepareAndPlay(mUrl);
        }

        mPlayer.setPreparedListener(new MediaPlayer.MediaPlayerPreparedListener() {
            @Override
            public void onPrepared() {
                //准备完成时触发
                Toast.makeText(AliPlayerActivity.this, "准备完成", Toast.LENGTH_SHORT).show();
            }
        });

        mPlayer.setFrameInfoListener(new MediaPlayer.MediaPlayerFrameInfoListener() {
            @Override
            public void onFrameInfoListener() {
                //首帧显示时触发
                Toast.makeText(AliPlayerActivity.this, "播放开始", Toast.LENGTH_SHORT).show();
            }
        });

        mPlayer.setErrorListener(new MediaPlayer.MediaPlayerErrorListener() {
            @Override
            public void onError(int i, String msg) {
                //错误发生时触发，错误码见接口文档
                Toast.makeText(AliPlayerActivity.this, "播放失败", Toast.LENGTH_SHORT).show();
            }
        });

        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
                holder.setKeepScreenOn(true);
                if (mPlayer != null) {
                    mPlayer.setVideoSurface(mSurfaceView.getHolder().getSurface());
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                if (mPlayer != null)
                    mPlayer.setSurfaceChanged();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });

    }

    private void initView() {
        mLayoutCommon = (RelativeLayout) findViewById(R.id.player_layout_common);
        mLayoutList = (RelativeLayout) findViewById(R.id.player_layout_list);
        mLayoutGroup = (LinearLayout) findViewById(R.id.player_layout_group);
        mScrollView = (ScrollView) findViewById(R.id.player_layout_scroll);

        mRecyclerView = (RecyclerView) findViewById(R.id.player_list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(new MyAdapter());

        findViewById(R.id.player_iv_bag).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLayoutCommon.setVisibility(View.GONE);
                mLayoutList.setVisibility(View.VISIBLE);
            }
        });

        mSurfaceView = (SurfaceView) findViewById(R.id.player_surface);
        mSurfaceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLayoutCommon.setVisibility(View.VISIBLE);
                mLayoutList.setVisibility(View.GONE);
            }
        });

        handler = new MyHandler(this);
        handler.sendEmptyMessage(123);

    }

    private void addChatMsg() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TextView textView = new TextView(AliPlayerActivity.this);
                textView.setTextColor(Color.WHITE);
                textView.setTextSize(15);
                textView.setText("有新用户进入了房间");
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(0, Utils.dip2px(10, AliPlayerActivity.this), 0, 0);
                if (mLayoutGroup.getChildCount() < 30) {
                    mLayoutGroup.addView(textView, layoutParams);
                }else {
                    mLayoutGroup.removeViewAt(0);
                    mLayoutGroup.addView(textView, layoutParams);
                }

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
                    }
                });

                handler.sendEmptyMessageDelayed(123, 5000);

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mPlayer != null) {
            mPlayer.stop();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPlayer != null) {
            mPlayer.prepareAndPlay(mUrl);
        }

    }

    @Override
    protected void onDestroy() {
        if (mPlayer != null) {
            mPlayer.destroy();
        }

        handler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }

    private class MyAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(View.inflate(AliPlayerActivity.this, R.layout.item_player_list, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 5;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            public MyViewHolder(View itemView) {
                super(itemView);
            }
        }
    }

    public static class MyHandler extends Handler {

        WeakReference<AliPlayerActivity> weakReference;

        MyHandler(AliPlayerActivity activity) {
            weakReference = new WeakReference<AliPlayerActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            AliPlayerActivity activity = weakReference.get();
            if (activity != null) {

                if (msg.what == 123) {
                    activity.addChatMsg();
                }
            }
        }
    }

}
